<?php

namespace App\Jobs\Cruise;

use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Remote\Rms\RmsShopRemote;
use App\Service\Cruise\Shop\CruiseShopService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class AgainstExpireCashOff implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $timeout = 1800;

    private int $againstId;

    /**
     * 违约函到期自动停收银
     *
     * @param int $againstId
     */
    public function __construct(int $againstId)
    {
        $this->againstId = $againstId;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info("AgainstExpireCashOff enter againstId:{$this->againstId}");

        $againstModel = CruiseCheckAgainstModel::query()->find($this->againstId);
        if (!$againstModel) {
            $this->delete();
            return;
        }

        // 判断状态是未处理的违约函
        if ($againstModel->status != CruiseCheckAgainstModel::STATUS_WAIT) {
            $this->delete();
            return;
        }

        Log::info("AgainstExpireCashOff doing againstId:{$this->againstId} shopId:{$againstModel->shopId} ");

        if ($againstModel->level == CruiseCheckAgainstModel::LEVEL_PUNISH) {//处罚
            // 自动停收银
            CruiseShopService::updateCashRegisterStatusByShopId($againstModel->shopId, RmsShopRemote::SHOP_CASH_REGISTER_STATUS_CHECK_OFF);
        }

        Log::info("AgainstExpireCashOff end againstId:{$this->againstId} shopId:{$againstModel->shopId} ");
    }
}
